package com.jerry.orm;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {

	private static Pattern UNDERLINE_PATTERN = Pattern.compile("_([a-z])");
	
	/**
	 * 字符串转为驼峰命名法
	 */
	public static String underLineToHump (String str){
		//正则匹配下划线及后一个字符串，删除下划线并将匹配字符串转为大写
		Matcher matcher = UNDERLINE_PATTERN.matcher(str);
		StringBuffer sb = new StringBuffer(str);
		//将之前匹配的字符串替换成指定的字符串，将替换后的字符串及上次匹配的字符串之后的字符串添加到StringBuffer对象
		if(matcher.find()){
			sb = new StringBuffer();
			//正则之前的字符串和被替换的字符串
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
			//把之后的字符串也添加到StringBuffer的对象中
			matcher.appendTail(sb);
		}else{
			//去除字母之外前面带的下划线
			return sb.toString().replaceAll("_", "");
		}
		return underLineToHump(sb.toString());
	}
	
	public static void main(String[] args) {
		System.out.println(underLineToHump("pay_settle_app_and_trans"));
	}
}
